package com.wsoft.dc.manager;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.dc.entity.DcConfigEntityEntity;
import com.wsoft.dc.mapper.DcConfigEntityMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/10/23 09:08
 */
@Service
public class DcConfigEntityManager extends ServiceImpl<DcConfigEntityMapper, DcConfigEntityEntity> {

    @Resource
    private RedisService redisService;

    @Override
    public boolean save(DcConfigEntityEntity entity){
        boolean b = super.save(entity);
        if(b){
            redisService.del(RedisKeyConstant.ConfigEntity.CONFIG_CONFIG_ID + entity.getConfigId());
        }
        return b;
    }

    /**
     * 根据配置id和属性名查询配置项
     * @param configId
     * @param attribute
     * @return
     */
    public DcConfigEntityEntity getConfigAttribute(Long configId,String attribute){
        QueryWrapper<DcConfigEntityEntity> query = new QueryWrapper<>();
        query.lambda().eq(DcConfigEntityEntity::getConfigId,configId)
                .eq(DcConfigEntityEntity::getAttribute,attribute);
        return getOne(query);
    }

    /**
     * 根据配置 和 key查询
     * @param configId
     * @return
     */
    public DcConfigEntityEntity getConfigKey(Long configId){
        QueryWrapper<DcConfigEntityEntity> query = new QueryWrapper<>();
        query.lambda().eq(DcConfigEntityEntity::getConfigId,configId)
                .eq(DcConfigEntityEntity::getIsKey,1);
        return getOne(query);
    }


    /**
     * 修改
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(DcConfigEntityEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.ConfigEntity.CONFIG_ENTITY_ID + entity.getId());
            redisService.del(RedisKeyConstant.ConfigEntity.CONFIG_CONFIG_ID + entity.getConfigId());

        }
        return b;
    }

    @GetRedisCache(type = RedisKeyConstant.ConfigEntity.CONFIG_ENTITY_ID,
            expire_sec = RedisKeyConstant.ConfigEntity.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public DcConfigEntityEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        DcConfigEntityEntity entity = this.getById(id);
        boolean b = super.removeById(id);
        if(b){
            redisService.del(RedisKeyConstant.ConfigEntity.CONFIG_ENTITY_ID + id);
            redisService.del(RedisKeyConstant.ConfigEntity.CONFIG_CONFIG_ID + entity.getConfigId());
        }
        return b;
    }

    /**
     * 根据configId 删除
     * @param configId
     * @return
     */
    public boolean removeByConfigId(Long configId){
        List<DcConfigEntityEntity> list = listConfigEntity(configId);
        if(CollUtil.isNotEmpty(list)){
            for (DcConfigEntityEntity entity : list) {
                removeById(entity.getId());
            }
        }
        return true;
    }

    /**
     * 列表查询
     * @param param
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.ConfigEntity.CONFIG_CONFIG_ID,
            expire_sec = RedisKeyConstant.ConfigEntity.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public List<DcConfigEntityEntity> listConfigEntity(Long configId) {
        QueryWrapper<DcConfigEntityEntity> query = new QueryWrapper<>();
        query.lambda().eq(DcConfigEntityEntity::getConfigId,configId)
                .orderByAsc(DcConfigEntityEntity::getSort);
        return list(query);
    }
}
